<?php
class DaikuanAction extends CommonAction{
	
	
	//借款列表
	public function index(){
		$this->title = "借款列表";
		$keyword = I("keyword",'','trim');
		$this->keyword = $keyword;
		$where = array();
		if($keyword){
			$where['user'] = array('like',"%{$keyword}%");
		}
		$Order = D("order");
		import('ORG.Util.Page');
		$count = $Order->where($where)->count();
		$Page  = new Page($count,25);
		$Page->setConfig('theme','共%totalRow%条记录 | 第 %nowPage% / %totalPage% 页 %upPage%  %linkPage%  %downPage%');
		$show  = $Page->show();
		$list = $Order->where($where)->order('addtime Desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->list = $list;
		$this->page = $show;
		$this->display();
	}
	
	//修改订单状态
	public function changestatus(){
		$id = I("id",0,'trim');
		$status = I("status",'','trim');
		$data = array('status' => 0,'msg' => '未知错误');
		if(!$id || $status == ''){
			$data['msg'] = "参数错误!";
		}else{
			$Order = D("order");
			$count = $Order->where(array('id' => $id))->count();
			if(!$count){
				$data['msg'] = "订单不存在!";
			}else{
				$on = $Order->where(['id'=>$id])->getField('ordernum');
				if($status == 2){
					M('payorder')->where(['ordernum'=>$on])->save(['status'=>1]);
				}else{
					M('payorder')->where(['ordernum'=>$on])->save(['status'=>0]);
					
				}
				
				$status = $Order->where(array('id' => $id))->save(array('status' => $status));


				$Order=M('Order')->where(array('id' => $id,'status' => 2))->order('id asc')->find();
				if($Order!=null){
					$user=$Order['user'];
					$jisuan_sum=M('user')->where(array('phone' => $user))->find();
					
					if($jisuan_sum['yao_phone']!=null&&$jisuan_sum['jisuan_ticheng']==0){
						$ticheng_sumxiangjia=M('user')->where(array('phone' => $jisuan_sum['yao_phone']))->find();
						$ketixian=$ticheng_sumxiangjia['ketixian']+$Order['money']*0.05;
					
					$ticheng_sum_user=$Order['money']*0.05;
					$u0=M('user')->where(array('phone' => $user))->save(array('ticheng_sum'=>$ticheng_sum_user));
				$u1=M('user')->where(array('phone' => $jisuan_sum['yao_phone']))->save(array('ketixian'=>$ketixian));
				
				  $u2=M('user')->where(array('phone' => $user))->save(array('jisuan_ticheng' => 1));
				  
					}
				}
				if(!$status){
					$data['msg'] = "操作失败!";
				}else{
					$data['status'] = 1;
				}
			}
		}
		$this->ajaxReturn($data);
	}
	
	//删除订单
	public function del(){
        $this->title='删除订单';
        $id = I('id',0,'trim');
        if(!$id){
            $this->error("参数有误!");
        }
        $Order = D("order");
        $status = $Order->where(array('id' => $id))->delete();
        if(!$status){
            $this->error("删除失败!");
        }
        $this->success("删除订单成功!");
	}
	
	/**
	 * 发送短信
	 */
	public function send_sms(){
		$this->title = "发送短信";
		
		$order_id=I('id',0,'trim');
		$order=M("order")->where("id=$order_id")->find();
		
		$list=M("order_sms")->where("order_id=$order_id")->select();
		
		$this->assign('order',$order);
		$this->assign('list',$list);
		$this->assign('order_id',$order_id);
		$this->display();
	}
	
	public function act_send_sms(){

		$Code=D("order_sms");
		$order_id=$_POST['order_id'];
		$phone=$_POST['mobile'];
		$content=$_POST['content'];
		/*
		$smsapi = "http://api.smsbao.com/";
		$user = C('cfg_smssid'); //短信平台帐号
		$pass = md5(C('cfg_smstoken')); //短信平台密码
		
		$sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
		
		$result =file_get_contents($sendurl) ;
		
		*/

		//吉信通
		$uid=C('cfg_smssid'); //分配给你的账号
		$pwd=C('cfg_smstoken'); //密码
		
		$c = rand(1111,9999);
		$qianming= C('cfg_smssign');
		$content = '【'.$qianming."】".$content;
		$contents=urlencode(auto_charset($content,"utf-8",'gbk'));  //短信内容
		//$contents='【创胜贷】您的审核已经通过';
		$sendurl="http://service.winic.org/sys_port/gateway/?id=".$uid."&pwd=".$pwd."&to=".$phone."&content=".$contents;
		
		$result =file_get_contents($sendurl) ;
		$datas = explode("/",$result);
		if($datas[0]=='000'){
			$result=0;
		}
		
		
		if($result==0){
			//写入验证码记录
			$Code->add(array(
				'type'=>2,
				'order_id'=>$order_id,
				'mobile'    => $phone,
				'sms_content'     => $content,
				'addtime' => time()
			));
		
			exit("1");
		}else{
			exit("0");
		}
	}

	function save_code(){
		$oid=$_POST['id'];
		$code=$_POST['code'];
		
		$ret=M("order")->where("id=$oid")->save(array('code'=>$code));
		if($ret){
			$data['status'] = 1;
		}else{
			$data['status'] = 0;
		}
		
		$this->ajaxReturn($data);
	}
	
	function save_change_money(){
		$oid=$_POST['id'];
		$money=$_POST['c_money'];
		
		$order=M("order")->where("id=$oid")->find();
		$month=$order['months'];
		
		$fuwufei = C('cfg_fuwufei');
		$fuwufei = explode(",",$fuwufei);
		
		$rixi = round($fuwufei[$month-1] / 30,2);
		$fuwufei = $fuwufei[$month-1] * $money / 100;
		$huankuan=ceil((float)($money/$month));
		
		$datas['money']=$money;
		//$datas['fuwufei']=$fuwufei;
		//$datas['huankuan']=ceil((float)($money/$month));
		$datas['monthmoney']=ceil($huankuan+$fuwufei);
		//$datas['rixi']=$rixi;
		//dump($datas);exit;
		$ret=M("order")->where("id=$oid")->save($datas);
		
		if($ret){
			$data['status'] = 1;
		}else{
			$data['status'] = 0;
		}
		
		$this->ajaxReturn($data);
	}
}

